/*
 * @Description:
 * @Author:  huangshuai
 * @Date: 2023-10-22 09:06:03
 * @LastEditTime: 2023-12-07 15:42:12
 * @LastEditors: huangshuai
 */
import { createRouter, createWebHashHistory } from "vue-router";
import { useUserStore } from "@/stores/user";
import { ElMessage } from "element-plus";
const routes = [
  {
    path: "/",
    redirect: "/Layout",
  },
  {
    path: "/Layout",
    component: () => import("../view/Layout/index.vue"),
    name: "layout",
    redirect: "/home",
    children: [
      {
        path: "/owner",
        name: "owner",
        component: () => import("../view/community-owner/index.vue"),
        redirect: "/home",
        meta: {
          title: "社区业主",
        },
        children: [
          {
            path: "/home",
            name: "/home",
            component: () => import("../view/home/index.vue"),
            meta: {
              title: "首页",
            },
          },
          {
            path: "pet-info",
            name: "owner/pet-info",
            component: () =>
              import("../view/community-owner/pet-info/index.vue"),
            meta: {
              title: "宠物信息",
            },
          },

          {
            path: "pet-loss-info",
            name: "owner/pet-loss-info",
            component: () =>
              import("../view/community-owner/pet-loss-info/index.vue"),
            meta: {
              title: "宠物挂失",
            },
          },
          {
            path: "person-info",
            name: "owner/person-info",
            component: () =>
              import("../view/community-owner/person-info/index.vue"),
            meta: {
              title: "个人信息修改",
            },
          },

          {
            path: "report-apply",
            name: "owner/report-apply",
            component: () =>
              import("../view/community-owner/report-others/index.vue"),
            meta: {
              title: "举报申请",
            },
          },
          {
            path: "report",
            name: "owner/report",
            component: () =>
              import("../view/community-owner/report-others/send-report.vue"),
            meta: {
              title: "举报他人",
            },
          },
          {
            path: "be-reported",
            name: "owner/be-report",
            component: () =>
              import("../view/community-owner/be-reported/index.vue"),
            meta: {
              title: "举报收信",
            },
          },
          {
            path: "post",
            name: "owner/post",
            component: () => import("../view/community-owner/post/index.vue"),
            meta: {
              title: "帖子",
            },
          },
          {
            path: "send-email",
            name: "owner/send-email",
            component: () => import("../view/send-email/index.vue"),
            meta: {
              title: "发送邮件",
            },
          },
        ],
      },
      {
        path: "/manager",
        name: "manager",
        component: () => import("../view/community-manager/index.vue"),
        redirect: "/home",
        meta: {
          title: "社区管理员",
        },
        children: [
          {
            path: "/home",
            name: "home",
            component: () => import("../view/home/index.vue"),
            meta: {
              title: "首页",
            },
          },
          {
            path: "report-desc",
            name: "manager/report-desc",
            component: () =>
              import("../view/community-manager/report-desc/index.vue"),
            meta: {
              title: "举报信息处理",
            },
          },

          {
            path: "owner-apply",
            name: "manager/owner-apply",

            component: () =>
              import("../view/community-manager/owner-apply/index.vue"),
            meta: {
              title: "用户申请处理",
            },
          },
          {
            path: "send-email",
            name: "manager/send-email",
            component: () => import("../view/send-email/index.vue"),
            meta: {
              title: "发送邮件",
            },
          },
          {
            path: "pet-manager",
            name: "manager/pet-manager",
            component: () =>
              import("../view/community-manager/pet-manager/index.vue"),
            meta: {
              title: "宠物管理",
            },
          },
        ],
      },
      {
        path: "/super",
        name: "super",
        component: () => import("../view/community-super/index.vue"),
        redirect: "/home",
        meta: {
          title: "超级管理员",
        },
        children: [
          {
            path: "/home",
            name: "home",
            component: () => import("../view/home/index.vue"),
            meta: {
              title: "首页",
            },
          },

          {
            path: "notice",
            name: "super/notice",
            component: () => import("../view/community-super/notice/index.vue"),
            meta: {
              title: "公告",
            },
          },
          {
            path: "edit-user",
            name: "super/edit-user",
            component: () =>
              import("../view/community-super/edit-user/index.vue"),
            meta: {
              title: "用户管理",
            },
          },
          {
            path: "send-email",
            name: "super/send-email",
            component: () => import("../view/send-email/index.vue"),
            meta: {
              title: "发送邮件",
            },
          },
          {
            path: "pet-manager",
            name: "super/pet-manager",
            component: () =>
              import("../view/community-super/pet-manager/index.vue"),
            meta: {
              title: "宠物管理",
            },
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    component: () => import("../view/login/index.vue"),
    meta: {
      title: "登录",
    },
  },
  // {
  //   path: "/team",
  //   component: () => import("../view/team/index.vue"),
  //   meta: {
  //     title: "团队介绍",
  //   },
  // },
  {
    path: "/register",
    component: () => import("../view/register/index.vue"),
    meta: {
      title: "注册",
    },
  },
  {
    path: "/404",
    name: "404",
    component: () => import("@/view/404/index.vue"),
    meta: {
      title: "404",
    },
  },
  {
    path: "/:catchAll(.*)", // 匹配除以上之外的所有路由
    redirect: "/404",
    meta: {
      title: "404",
    },
  },
];

const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes,
});
router.beforeEach(async (to, from, next) => {
  // 用户信息
  const userStore = useUserStore();
  // alert(from.path);
  if (from.path !== "/login" && to.path === "/login" && userStore.isLogin) {
    ElMessage({
      message: "已退出登录，请重新登录.",
      type: "warning",
    });
  }

  if (to.path !== "/login" && to.path !== "/register" && !userStore.isLogin) {
    next("/login");
    return;
  }

  // if (!router.getRoutes().find((item: any) => item.path === "/home")) {
  //   alert(userStore.userDesc.userType);
  //   userStore.roleMenu = await userStore.handleUserMenu(
  //     userStore.userDesc.userType
  //   );
  //   next({ ...to, replace: true });
  // }
  document.title = to.meta.title as string;
  next();
});

export default router;
